package com.cskaoyan.service;


import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.GoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * zx
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public GoodsVO queryGoods(BaseParam baseParam, Integer goodsId, String goodsSn, String name) {
        // 自己逻辑:
        if(baseParam.getLimit() == 0  && baseParam.getPage() == null){
            return  new GoodsVO();
        }

        GoodsVO goodsVO = new GoodsVO();
        PageHelper.startPage(baseParam.getPage() , baseParam.getLimit());
        List<GoodsPO> goodsPOS = goodsMapper.selectGoods(baseParam.getSort(), baseParam.getOrder(), goodsId, goodsSn,
                name);
        List<GoodsListVO> goodsListVOS = new ArrayList<>();
        for (GoodsPO goodsPO : goodsPOS) {
            GoodsListVO goodsListVO = new GoodsListVO();
            goodsListVO.setAddTime(goodsPO.getAddTime());
            goodsListVO.setBrandId(goodsPO.getBrandId());
            goodsListVO.setBrief(goodsPO.getBrief());
            goodsListVO.setCategoryId(goodsPO.getCategoryId());
            goodsListVO.setCounterPrice(goodsPO.getCounterPrice());
            goodsListVO.setDeleted(goodsPO.getDeleted());
            goodsListVO.setGallery(goodsPO.getGallery());
            goodsListVO.setGoodsSn(goodsPO.getGoodsSn());
            goodsListVO.setId(goodsPO.getId());
            goodsListVO.setIsHot(goodsPO.getIsHot());
            goodsListVO.setIsNew(goodsPO.getIsNew());
            goodsListVO.setIsOnSale(goodsPO.getIsOnSale());
            goodsListVO.setKeywords(goodsPO.getKeywords());
            goodsListVO.setName(goodsPO.getName());
            goodsListVO.setPicUrl(goodsPO.getPicUrl());
            goodsListVO.setRetailPrice(goodsPO.getRetailPrice());
            goodsListVO.setSortOrder(goodsPO.getSortOrder());
            goodsListVO.setUnit(goodsPO.getUnit());
            goodsListVO.setUpdateTime(goodsPO.getUpdateTime());
            goodsListVOS.add(goodsListVO);
        }
        PageInfo pageInfo = new PageInfo(goodsPOS);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        goodsVO.setTotal((int) total);
        goodsVO.setPage(baseParam.getPage());
        goodsVO.setPages(pages);
        goodsVO.setLimit(baseParam.getLimit());
        goodsVO.setList(goodsListVOS);
        return goodsVO;
    }

    @Override
    public GoodsCreateVO createGoods(GoodsBO goodsBO) {
//        GoodsPO goodsListVO = new GoodsPO();
        BigDecimal price = null;
        for (GoodsProductBO productBO : goodsBO.getProducts()) {
            if (productBO.getPrice() == null) {
                price = BigDecimal.valueOf(0);
            } else {
                price = productBO.getPrice();
            }
        }
        GoodListBO listBO = goodsBO.getGoods();
        GoodsListPO goodListBO = new GoodsListPO();
        GoodsCreateVO createVO = new GoodsCreateVO();
        goodListBO.setBrandId(listBO.getBrandId());
        goodListBO.setBrief(listBO.getBrief());
        goodListBO.setCategoryId(listBO.getCategoryId());
        goodListBO.setCounterPrice(listBO.getCounterPrice());
        goodListBO.setDetail(listBO.getDetail());
        goodListBO.setGallery(listBO.getGalleryString());
        goodListBO.setGoodsSn(listBO.getGoodsSn());
        goodListBO.setIsHot(listBO.getHot());
        goodListBO.setIsNew(listBO.getNew());
        goodListBO.setIsOnSale(listBO.getOnSale());
        goodListBO.setKeywords(listBO.getKeywords());
        goodListBO.setName(listBO.getName());
        goodListBO.setPicUrl(listBO.getPicUrl());
        goodListBO.setUnit(listBO.getUnit());
        goodListBO.setAddTime(new Date());
        goodListBO.setUpdateTime(new Date());
        goodListBO.setRetailPrice(price);
        // table1
        int count = goodsMapper.insertGoods(goodListBO);

        List<GoodsAttributeBO> attributes = goodsBO.getAttributes();
        for (GoodsAttributeBO attribute : attributes) {
            attribute.setGoodsId(Integer.parseInt(listBO.getGoodsSn()));
            attribute.setAddTime(goodListBO.getAddTime());
            attribute.setUpdateTime(goodListBO.getUpdateTime());
            goodsMapper.insertAttributes(attributes);
        }

        List<GoodsProductBO> goodsProductBOS = goodsBO.getProducts();
        for (GoodsProductBO goodsProductBO : goodsProductBOS) {
            goodsProductBO.setGoodsId(Integer.parseInt(listBO.getGoodsSn()));
            goodsProductBO.setUrl(listBO.getPicUrl());
            goodsProductBO.setAddTime(goodListBO.getAddTime());
            goodsProductBO.setUpdateTime(goodListBO.getUpdateTime());
            GoodsProductsPO goodsProductsPO = new GoodsProductsPO(goodsProductBO.getId(), goodsProductBO.getGoodsId(),
                    goodsProductBO.getSpecificationString(), goodsProductBO.getPrice(), goodsProductBO.getNumber(),
                    goodsProductBO.getUrl(),
                    goodsProductBO.getAddTime(), goodsProductBO.getUpdateTime(), goodsProductBO.getDeleted()
            );
            goodsMapper.insertProduct(goodsProductsPO);
        }

        List<GoodsSpecificationBO> specificationBOS = goodsBO.getSpecifications();
        for (GoodsSpecificationBO specificationBO : specificationBOS) {
            specificationBO.setGoodsId(Integer.parseInt(listBO.getGoodsSn()));
            specificationBO.setAddTime(goodListBO.getAddTime());
            specificationBO.setUpdateTime(goodListBO.getUpdateTime());
            goodsMapper.insertSpecification(specificationBOS);
        }
        createVO.setAttributes(goodsBO.getAttributes());
        createVO.setGoods(goodsBO.getGoods());
        createVO.setProducts(goodsBO.getProducts());
        createVO.setSpecifications(goodsBO.getSpecifications());
        return createVO;
    }

    @Override
    public GoodsUpdateVO updateGoods(GoodsUpdateGetBO goodsUpdateGetBO) {
        GoodsUpdateVO goodsUpdateVO = new GoodsUpdateVO();
        GoodsUpdateBO listBO = goodsUpdateGetBO.getGoods();
        GoodsListPO goodListBO = new GoodsListPO();
        goodListBO.setId(listBO.getId());
        goodListBO.setRetailPrice(listBO.getRetailPrice());
        goodListBO.setShareUrl(listBO.getShareUrl());
        goodListBO.setBrandId(listBO.getBrandId());
        goodListBO.setBrief(listBO.getBrief());
        goodListBO.setCategoryId(listBO.getCategoryId());
        goodListBO.setCounterPrice(listBO.getCounterPrice());
        goodListBO.setDetail(listBO.getDetail());
        goodListBO.setGallery(listBO.getGalleryString());
        goodListBO.setGoodsSn(listBO.getGoodsSn());
        goodListBO.setIsHot(listBO.getHot());
        goodListBO.setIsNew(listBO.getNew());
        goodListBO.setIsOnSale(listBO.getOnSale());
        goodListBO.setKeywords(listBO.getKeywords());
        goodListBO.setName(listBO.getName());
        goodListBO.setPicUrl(listBO.getPicUrl());
        goodListBO.setUnit(listBO.getUnit());
//        goodListBO.setAddTime(new Date());
        goodListBO.setUpdateTime(new Date());
        goodListBO.setSortOrder(listBO.getSortOrder());
        int count = goodsMapper.updateGoods(goodListBO);

        List<GoodsAttributeBO> goodsAttributeBOS = goodsUpdateGetBO.getAttributes();
        for (GoodsAttributeBO goodsAttributeBO : goodsAttributeBOS) {
            goodsAttributeBO.setGoodsId(listBO.getId());
            goodsAttributeBO.setAddTime(goodListBO.getAddTime());
            goodsAttributeBO.setUpdateTime(goodListBO.getUpdateTime());
            GoodsAttributePO po = new GoodsAttributePO(goodsAttributeBO.getId(), goodsAttributeBO.getGoodsId(),
                    goodsAttributeBO.getAttribute(),
                    goodsAttributeBO.getValue(), goodsAttributeBO.getAddTime(),
                    goodsAttributeBO.getUpdateTime(), goodsAttributeBO.getDeleted());
            int count2 = goodsMapper.updateAttribute(po);
        }
        List<GoodsProductBO> goodsProductBOS = goodsUpdateGetBO.getProducts();
        for (GoodsProductBO goodsProductBO : goodsProductBOS) {
            goodsProductBO.setGoodsId(listBO.getId());
            goodsProductBO.setUrl(listBO.getPicUrl());
            goodsProductBO.setAddTime(goodListBO.getAddTime());
            goodsProductBO.setUpdateTime(goodListBO.getUpdateTime());
            GoodsProductsPO po = new GoodsProductsPO(goodsProductBO.getId(), goodsProductBO.getGoodsId(),
                    goodsProductBO.getSpecificationString(), goodsProductBO.getPrice(), goodsProductBO.getNumber(),
                    goodsProductBO.getUrl(),
                    goodsProductBO.getAddTime(), goodsProductBO.getUpdateTime(), goodsProductBO.getDeleted());
            int count3 = goodsMapper.updateProduct(po);
        }

        List<GoodsSpecificationBO> specificationBOS = goodsUpdateGetBO.getSpecifications();
        for (GoodsSpecificationBO specificationBO : specificationBOS) {
            specificationBO.setGoodsId(listBO.getId());
            specificationBO.setAddTime(goodListBO.getAddTime());
            specificationBO.setUpdateTime(goodListBO.getUpdateTime());
            GoodsSpecificationPO po = new GoodsSpecificationPO(
                    specificationBO.getId(), specificationBO.getGoodsId(),
                    specificationBO.getSpecification(), specificationBO.getValue(),
                    specificationBO.getPicUrl(), specificationBO.getAddTime(),
                    specificationBO.getUpdateTime(), specificationBO.getDeleted()
            );
            int count4 = goodsMapper.updateSpecification(po);
        }
        goodsUpdateVO.setAttributes(goodsUpdateGetBO.getAttributes());
        goodsUpdateVO.setGoods(goodsUpdateGetBO.getGoods());
        goodsUpdateVO.setProducts(goodsUpdateGetBO.getProducts());
        goodsUpdateVO.setSpecifications(goodsUpdateGetBO.getSpecifications());
        return goodsUpdateVO;
    }

    @Override
    public void deleteGoods(GoodsListPO goodListBO) {
        int count = goodsMapper.deleteGoods(goodListBO);
    }

    @Override
    public GoodsDetailVO detailGoods(Integer id) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        Integer goodsId = goods.getId();
        String goodsSn = goods.getGoodsSn();
        Integer categoryId = goods.getCategoryId();
        String gallery = goods.getGallery();
        String replace = gallery.replace("\"", "");
        List<String> split = Arrays.asList(replace.substring(1, replace.length() - 1).split(", "));

        GoodsDetailVO detailVO = new GoodsDetailVO();
        GoodsListVO2 goodsListVO2 = new GoodsListVO2(goods.getId(), goods.getGoodsSn(), goods.getName(),
                goods.getCategoryId(), goods.getBrandId(), split, goods.getKeywords(), goods.getBrief(),
                goods.getIsOnSale(), goods.getSortOrder(), goods.getPicUrl(), goods.getShareUrl(), goods.getIsNew(),
                goods.getIsHot(), goods.getUnit(), goods.getCounterPrice(), goods.getRetailPrice(),
                goods.getAddTime(), goods.getUpdateTime(), goods.getDeleted(), goods.getDetail());
        //goods
        detailVO.setGoods(goodsListVO2);

        List<GoodsSpecificationBO> goodsSpecificationBOS = goodsMapper.selectByGoodsId1(String.valueOf(goodsId));
        detailVO.setSpecifications(goodsSpecificationBOS);

        List<GoodsAttributeBO> attributeBOS = goodsMapper.selectByGoodsId2(String.valueOf(goodsId));
        detailVO.setAttributes(attributeBOS);

        List<GoodsProductPO> goodsProductBOS = goodsMapper.selectByGoodsId3(String.valueOf(goodsId));
        List<GoodsProductBO> goodsProductBOS1 = new ArrayList<>();
        for (GoodsProductPO goodsProductBO : goodsProductBOS) {
            String specifications = goodsProductBO.getSpecifications();
            String substring = specifications.substring(2, specifications.length() - 2);
            List<String> strings = new ArrayList<>();
            strings.add(substring);
            GoodsProductBO goodsProductBO1 = new GoodsProductBO(goodsProductBO.getId(), goodsProductBO.getGoodsId(),
                    strings, goodsProductBO.getPrice(), goodsProductBO.getNumber(), goodsProductBO.getUrl(),
                    goodsProductBO.getAddTime(), goodsProductBO.getUpdateTime(), goodsProductBO.getDeleted(), null);
            goodsProductBOS1.add(goodsProductBO1);
        }
        detailVO.setProducts(goodsProductBOS1);

        List<Integer> categoryIds = detailVO.getCategoryIds();
        Category category = goodsMapper.selectByCategory(categoryId);
        if (category != null) {
            categoryIds.add(category.getPid());
            categoryIds.add(category.getId());
        }

        detailVO.setCategoryIds(categoryIds);
        return detailVO;
    }

    @Override
    public GoodsCatAndBrandVO getCatAndBrand() {
        GoodsCatAndBrandVO catAndBrandVO = new GoodsCatAndBrandVO();
        List<GoodsCatAndBrandCategoryVO> brandList = goodsMapper.getBrand();

        catAndBrandVO.setBrandList(brandList);

        List<GoodsCatAndBrandBrandVO> categoryVOList = goodsMapper.getCategory(0);
        for (GoodsCatAndBrandBrandVO goodsCatAndBrandCategoryVO : categoryVOList) {
            Integer parentCategoryId = goodsCatAndBrandCategoryVO.getValue();
            List<GoodsCatAndBrandCategoryChildrenVO> children = new ArrayList<>();
            children = goodsMapper.getChilderCategory(parentCategoryId);
            goodsCatAndBrandCategoryVO.setChildren(children);
        }
        catAndBrandVO.setCategoryList(categoryVOList);
        return catAndBrandVO;
    }
}
